package com.vancouver.knows.ui.activity.search;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;

import com.pangu.task.mark.ATaskMark;
import com.pangu.ui.refresh.ARefreshViewController;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.base.BaseActivity;
import com.vancouver.knows.business.BusinessModule;

public class SearchResultActivity extends BaseActivity {

	private Context mContext;
	@Bind(R.id.common_tips)
	TextView  mCommonTips;
	@Bind(R.id.common_back)
	ImageView mCommonBack;
	@Bind(R.id.search_result_layout)
	LinearLayout mLinearLayout;
	
	private String keyWord;
	private ARefreshViewController mController;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_result_activity);
		mContext = SearchResultActivity.this;
		setViews();
	}

	private void setViews() {
		Intent intent = getIntent();
		String totalCount = intent.getStringExtra("TOTAL_SEARCH_RESULT");
		String type = intent.getStringExtra("SEARCH_TYPE");
	    keyWord = intent.getStringExtra("SEARCH_KEYWORD");
		String searchResultTips = mContext.getString(R.string.search_total_result, Integer.valueOf(totalCount)) 
				+ "<font color=\"#ff0000\">"+ keyWord + "</font>" 
				+ mContext.getString(R.string.search_result);
		
		mCommonTips.setText(Html.fromHtml(searchResultTips));
		loadView(type);
	}
	
	private void loadView(String type){
		mController = new SearchResultPullRefreshView(mContext);
		((SearchResultPullRefreshView) mController).loadData(keyWord, type);
		mLinearLayout.addView(mController);
	}

	@OnClick(R.id.common_back)
	public void onReturnBackClick(){
		try {
			 BusinessModule bModule = ((MainApplication) imContext).getBusinessModule();
			 ATaskMark aTaskMark = bModule.getTaskMarkPool().getSearchResultTaskMark();
			 bModule.getCacheManager().getSearchResultCache().deleteItemInfoIndexFromCache(aTaskMark);
			 bModule.getCacheManager().getSearchResultCache().clearCache();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		mLinearLayout.removeAllViews();
		finish();
	}
}